package com.junchenrobot.mes.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.junchenrobot.mes.domain.InputTask;
import com.junchenrobot.mes.domain.WinderTask;
import com.junchenrobot.mes.domain.WinderTaskRun;
import com.junchenrobot.mes.domain.WinderTaskSpare;
import com.junchenrobot.mes.dto.AgvWorkInfoDto;
import com.junchenrobot.mes.mapper.WinderTaskSpareMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 备用任务Dao
 *
 * @author 
 * @date 202/2/11
 */
@Slf4j
@Component
public class WinderTaskSpareDao {
    
    private final WinderTaskSpareMapper winderTaskSpareMapper;
    
    @Autowired
    public WinderTaskSpareDao(WinderTaskSpareMapper winderTaskSpareMapper) {
        this.winderTaskSpareMapper = winderTaskSpareMapper;
    }

    public Long selectCount(){
        return  winderTaskSpareMapper.selectCount(new QueryWrapper<WinderTaskSpare>());

    }

    /**
     * 插入一条备选任务
     *
     * @param winderTaskSpare 备选任务
     * @return int
     */
    public int insertNewTask(WinderTaskSpare winderTaskSpare) {
        return winderTaskSpareMapper.insert(winderTaskSpare);
    }
    /**
     * 获取任务
     *
     * @param taskCode 任务代码
     * @return {@link InputTask}
     */
    public WinderTaskSpare getTaskSpare(String taskCode) {
        return new LambdaQueryChainWrapper<>(winderTaskSpareMapper).eq(WinderTaskSpare::getTaskCode, taskCode).last("LIMIT 1").one();
    }

    /**
     * 查询所有备用的任务
     *
     * @return {@link List}<{@link WinderTaskSpare}>
     */
    public List<WinderTaskSpare> listUnFinishedTask() {
        return new LambdaQueryChainWrapper<>(winderTaskSpareMapper)
                .orderByAsc(WinderTaskSpare::getTaskWarnTime)
                 .list();
    }

    /**
     * 删除任务
     *
     * @param winderTaskSpare 任务
     */
    public void deleteWinderTask(WinderTaskSpare winderTaskSpare) {
        int i = winderTaskSpareMapper.deleteById(winderTaskSpare);
        log.info("删除备用列表任务成功!{}",i);
    }


public void deleteAll(){
        winderTaskSpareMapper.deleteAll();
}

}
